package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.common.MD5Utils;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.view.BrowserLayout;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.Logs;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import es.voghdev.pdfviewpager.library.VerticalPDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;

/**
 * Created by xx on 2017/6/17.
 */
public class WebViewPDFTest extends BaseActivity{
    private BrowserLayout bl_loading_pdf;
    private VerticalPDFViewPager verticalPDF_viewpager;
    private String filePath;
    private File file;
    private PDFPagerAdapter adapter;
    private String protocol_name;
    private String protocol_url = "defalut";

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_webview_pdf_test;
    }

    @Override
    protected void initViewsAndEvents() {
        toolbar_title.setText(protocol_name);
        verticalPDF_viewpager = findView(R.id.verticalPDF_viewpager);
        bl_loading_pdf = findView(R.id.bl_loading_pdf);
        filePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + MD5Utils.getMD5String(protocol_url)+ "protocol.pdf";
        Logs.e("filePath: " + filePath);
        file = new File(filePath);
        if (file.exists()){
            Logs.e("存在");
            if (Build.VERSION.SDK_INT >= 21 ){
                adapter = new PDFPagerAdapter(mContext, filePath);
                verticalPDF_viewpager.setAdapter(adapter);
            } else {
                bl_loading_pdf.loadUrl(protocol_url);
                return;
            }

        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    downLoadFile(protocol_url);
                }
            }).start();

        }

    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        protocol_name = extras.getString("protocol_name");
        protocol_url = extras.getString("protocol_url");
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    private void downLoadFile(String url) {
        HttpClient httpClient1 = new DefaultHttpClient();
        HttpGet httpGet1 = new HttpGet(
                url);
        try {
            HttpResponse httpResponse1 = httpClient1.execute(httpGet1);

            StatusLine statusLine = httpResponse1.getStatusLine();
            if (statusLine.getStatusCode() == 200) {
                // 文件路径
                filePath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/protocol.pdf";
                file = new File(filePath);
                FileOutputStream outputStream = new FileOutputStream(file);
                InputStream inputStream = httpResponse1.getEntity()
                        .getContent();
                byte b[] = new byte[1024];
                int j = 0;
                while ((j = inputStream.read(b)) != -1) {
                    Logs.e("1");
                    mHandler.sendEmptyMessage(DOWN_JINDU);
                    outputStream.write(b, 0, j);
                }
                Logs.e("2");
                mHandler.sendEmptyMessage(DOWN_OVER);
                outputStream.flush();
                outputStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient1.getConnectionManager().shutdown();
        }
    }

    private final int DOWN_JINDU = 0;
    private final int DOWN_OVER = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DOWN_JINDU:
                    break;
                case DOWN_OVER:
                    if (Build.VERSION.SDK_INT >= 21 ){
                        adapter = new PDFPagerAdapter(mContext, filePath);
                        verticalPDF_viewpager.setAdapter(adapter);
                    }else{
                        bl_loading_pdf.loadUrl(protocol_url);
                        return;
                    }

                    break;
                default:
                    break;
            }
        }
    };
}
